using CSRedis;
using Gateway.Common.Enums;
using Gateway.IApplication.SystemSetting;
using Gateway.IApplication.SystemSetting.Input;
using Gateway.IQuery.SystemSetting;
using Gateway.IQuery.SystemSetting.Result;
using Gateway.UI.Api.Filters.Attributes;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.SignalR;
using Microsoft.EntityFrameworkCore;
using NewLife.Caching;
using Quartz;
using SqlSugar;

namespace Gateway.UI.Api.Controllers;

/// <summary>
/// 系统设置服务
/// </summary>
[AllowAnonymous]
public class SystemSettingController(ISystemSettingApp app, ISystemSettingQuery query) : BaseController
{
    /// <summary>
    /// 设置
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpPost, Route("set"), Log(logType: LogTypeEnum.SystemSetting, logEvent: "进行了系统设置")]
    public async Task<bool> Set(CreateSystemSettingInput input)
    {
        return await app.Set(input);
    }


    /// <summary>
    /// 获取系统配置详情
    /// </summary>
    /// <returns></returns>
    [HttpGet, Route("detail")]
    public async Task<SystemSettingDetailResult> GetDetail()
    {
        var data = await query.GetDetail();
        return data;
    }




    /// <summary>
    /// 获取服务器配置信息
    /// </summary>
    /// <returns></returns>
    [HttpGet("server/info")]
    public async Task<SystemServerResult> GetServerBase()
    {
        return await query.GetServerInfo();
    }

    /// <summary>
    /// 获取服务器使用信息
    /// </summary>
    /// <returns></returns>
    [HttpGet("server/used")]
    public async Task<SystemServerUsedResult> GetServerUsed()
    {
        return await query.GetServerUsed();
    }

    /// <summary>
    /// 获取服务器磁盘信息
    /// </summary>
    /// <returns></returns>
    [HttpGet("server/disk")]
    public async Task<List<SystemDiskResult>> GetServerDisk()
    {
        return await query.GetDiskInfoAsync();
    }

    /// <summary>
    ///  获取服务器程序集信息
    /// </summary>
    /// <returns></returns>
    [HttpGet("server/assembly")]
    public Task<List<AssemblyResult>> GetServerAssembly()
    {
        var sqlSugarAssembly = typeof(ISqlSugarClient).Assembly.GetName();
        var jsonAssembly = typeof(NewtonsoftJsonMvcCoreBuilderExtensions).Assembly.GetName();
        var quartzAssembly = typeof(ScheduleBuilder<>).Assembly.GetName();
        var efCoreAssembly = typeof(DbContext).Assembly.GetName();
        var csRedisAssembly = typeof(CSRedisClient).Assembly.GetName();
        var singalRAssembly = typeof(IHubCallerClients).Assembly.GetName();
        var httpAssembly = typeof(System.Net.Http.HttpClient).Assembly.GetName();
        var newLifeRedisAssembly = typeof(FullRedis).Assembly.GetName();

        return Task.FromResult(new List<AssemblyResult>()
        {
            new()
            {
                Name = sqlSugarAssembly.Name,
                Version = sqlSugarAssembly.Version
            },
            new(){
                Name = jsonAssembly.Name,
                Version = jsonAssembly.Version
            },
            new(){
                Name = quartzAssembly.Name,
                Version = quartzAssembly.Version
            },
            new(){
                Name = efCoreAssembly.Name,
                Version = efCoreAssembly.Version
            },
            new(){
                Name = csRedisAssembly.Name,
                Version = csRedisAssembly.Version
            },
            new(){
                Name = httpAssembly.Name,
                Version = httpAssembly.Version
            },
            new(){
                Name = singalRAssembly.Name,
                Version = singalRAssembly.Version
            },
            new(){
                Name = newLifeRedisAssembly.Name,
                Version = newLifeRedisAssembly.Version
            }
        });
    }
}